﻿using System;
using System.Collections.Generic;
using System.IO;
using System.ServiceProcess;
using IMDBApi.Implementation;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IMDBApi.Tests.UnitTestImplementation
{
    [TestClass]
    public class UnitTestPollingService
    {
        DirectoryInfo  _testDirectoryInfo;

        [TestInitialize]
        public void StartUp()
        {
          _testDirectoryInfo =  Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "Movies"));
        }

        [TestMethod]
        public void TestPollingStartAndStop()
        {
            //given
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "12.Years.a.Slave.2013.BDRip.MD.German.x264-POE.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "13.000.Kilometer.Die.Maenner.der.Emden.2012.German.1080p.BluRay.x264-ENCOUNTERS.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "13.Eerie.We.prey.for.you.2013.German.DL.1080p.BluRay.x264-ENCOUNTERS.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Absentia.2011.German.DL.720p.BluRay.x264-LeetHD.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Alice.im.Wunderland.3D.2010.German.DL.1080p.BluRay.x264.MerryXmas-ETM.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Apokalypse.Pompeii.3D.2014.German.DL.1080p.BluRay.x264-iFPD.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "As.I.Lay.Dying.2013.German.DL.1080p.BluRay.x264-ENCOUNTERS.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Bad.Country.2014.German.DTS-HD.DL.1080p.BluRay.AVC.REMUX-LeetHD.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Battle.of.the.Damned.2013.German.DTS.DL.1080p.BluRay.x264-RHD.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Blood.Creek.2009.German.DL.720p.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Cabin.Fever.3D.2002.German.DL.1080p.BluRay.x264-MAJESTiC.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Captain.Phillips.German.DL.1080p.BluRay.x264-EXQUiSiTE.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Centurion.Fight.or.Die.German.DL.1080p.BluRay.x264-EXQUiSiTE.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Charlie.Countryman.2013.German.DTS.DL.1080p.BluRay.x264-LeetHD.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Come.out.and.Play.2012.German.DL.1080p.BluRay.x264.mkv")).Close();

            //when
            PollingService service = new PollingService(_testDirectoryInfo.FullName);
            service.Start();
            Assert.IsTrue(service.IsStarted);
            service.Stop();

            //then
            Assert.IsFalse(service.IsStarted);
        }

        [TestMethod]
        public void TestPollingCheckAllFiles()
        {
            //given
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "12.Years.a.Slave.2013.BDRip.MD.German.x264-POE.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "13.000.Kilometer.Die.Maenner.der.Emden.2012.German.1080p.BluRay.x264-ENCOUNTERS.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "13.Eerie.We.prey.for.you.2013.German.DL.1080p.BluRay.x264-ENCOUNTERS.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Absentia.2011.German.DL.720p.BluRay.x264-LeetHD.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Alice.im.Wunderland.3D.2010.German.DL.1080p.BluRay.x264.MerryXmas-ETM.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Apokalypse.Pompeii.3D.2014.German.DL.1080p.BluRay.x264-iFPD.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "As.I.Lay.Dying.2013.German.DL.1080p.BluRay.x264-ENCOUNTERS.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Bad.Country.2014.German.DTS-HD.DL.1080p.BluRay.AVC.REMUX-LeetHD.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Battle.of.the.Damned.2013.German.DTS.DL.1080p.BluRay.x264-RHD.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Blood.Creek.2009.German.DL.720p.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Cabin.Fever.3D.2002.German.DL.1080p.BluRay.x264-MAJESTiC.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Captain.Phillips.German.DL.1080p.BluRay.x264-EXQUiSiTE.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Centurion.Fight.or.Die.German.DL.1080p.BluRay.x264-EXQUiSiTE.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Charlie.Countryman.2013.German.DTS.DL.1080p.BluRay.x264-LeetHD.mkv")).Close();
            File.Create(Path.Combine(_testDirectoryInfo.FullName, "Come.out.and.Play.2012.German.DL.1080p.BluRay.x264.mkv")).Close();

            //when
            PollingService service = new PollingService(_testDirectoryInfo.FullName);
            service.Start();
            service.CheckAllFiles();
            List<FileInfo> unratedFiles = PollingUtils.GetUnratedFiles(_testDirectoryInfo.FullName);

            //then
            Assert.IsTrue(unratedFiles.Count == 0);
        }

        [TestMethod]
        public void TestServiceControlling()
        {
            try
            {
                ServiceController controller = new ServiceController("IMDBApi", Environment.MachineName);
            }
            catch
            {

            }
        }

        [TestCleanup]
        public void CleanUp()
        {
            _testDirectoryInfo.Delete(true);
        }
    }
}
